package com.graduation.rescuematerial.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.rescuematerial.mapper.ApplyMaterialMapper;
import com.graduation.rescuematerial.mapper.CompanyMaterialMapper;
import com.graduation.rescuematerial.pojo.ApplyMaterial;
import com.graduation.rescuematerial.pojo.CompanyMaterial;
import com.graduation.rescuematerial.service.ApplyMaterialService;
import com.graduation.rescuematerial.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CaiShuangLian
 * @since 2022-04-22
 */
@Service
public class ApplyMaterialServiceImpl extends ServiceImpl<ApplyMaterialMapper, ApplyMaterial> implements ApplyMaterialService {

    @Autowired
    private ApplyMaterialMapper applyMaterialMapper;
    @Autowired
    private CompanyMaterialMapper companyMaterialMapper;
//    @Autowired
//    private MaterialMapper materialMapper;
//    @Autowired
//    private

    @Override
    public RespBean getAll() {
        return RespBean.success(applyMaterialMapper.getAll());
    }

    @Override
    public RespBean unionQuery(FuzzyOrderQueryVo fuzzyOrderQueryVo) {
        return RespBean.success(applyMaterialMapper.unionQuery(fuzzyOrderQueryVo));
    }

    @Override
    public RespBean applyMaterial(ApplyMaterialVo applyMaterialVo) {
        //获取该物资可申请的数量
        int row=applyMaterialMapper.applyMaterial(applyMaterialVo);
        if(row<1){
            return RespBean.error(RespBeanEnum.INSERT_ERROR,"申请失败，请检查网络");
        }
        return RespBean.success();
    }

//    @Override
//    public RespBean checkList(String companyName,String orderStatus) {
//        //根据orderStatus返回不同的列表
//        //arrive,cancel,forbidden(over)
//        // preparing,(待发货)
//        // way(待收货)
//        if(orderStatus.equals("preparing")){
//            return RespBean.success(applyMaterialMapper.checkPending(companyName,orderStatus));
//        }else if(orderStatus.equals("over")){
//            return RespBean.success();
//        }else if(orderStatus.equals("way")) {
//            return RespBean.success();
//        }
//        return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非法传值");
//    }

    @Override
    public RespBean getAppletOrder(String companyName,Long phone,String status) {
//        try{
//            applyMaterialMapper.getOrderByCompanyNameOrPhone(companyName,phone,status);
//        }catch (Exception e){
//
//            e.printStackTrace();
//        }
        return RespBean.success(applyMaterialMapper.getOrderByCompanyNameOrPhone(companyName,phone,status));
    }

    @Override
    public RespBean updateStatus(String companyType, Integer orderId) {
        //首先仓库和组织成员都可以修改物资状态
        //arrive,cancel,forbidden(over)
        // preparing,(待发货)
        // way(待收货)
        //初始状态：preparing
        //终止状态：over
        //preparing->cancel,forbidden,way
        //way->arrive

        //获取当前订单状态
        String currentStatus=applyMaterialMapper.getStatusById(orderId);
        if(!currentStatus.equals("preparing")){
            return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION,"当前订单状态不可更改");
        }
        String status=null;
        if(companyType.equals("warehouse")){
            status="forbidden";
        }else if(companyType.equals("department")){
            status="cancel";
        }
        if(applyMaterialMapper.updateStatus(orderId,status)<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"修改状态失败");
        }
        return RespBean.success();
    }

    @Override
    public RespBean deliver(DeliverVo deliverVo,Long phone) {

        Date date=new Date(System.currentTimeMillis());
        int row=applyMaterialMapper.deliver(deliverVo,date,"way",phone);
        if(row<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"订单更改失败");
        }
        //仓库减库存
        //首先获取该订单的全部信息
        ApplyMaterial applyMaterial=applyMaterialMapper.selectById(deliverVo.getOrderId());
        Integer materialId=applyMaterial.getMaterialId();//获取物资id
        Integer number=applyMaterial.getApplyNumber();//申请的数量
        String companyName=applyMaterial.getWarehouseName();//减库存的仓库
        int row1=companyMaterialMapper.reduceStock(companyName,materialId,number);
        if(row1<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"订单更改失败");
        }

        return RespBean.success();
    }

    @Override
    public RespBean receiving(Long userPhone,Integer id) {

        Date date=new Date(System.currentTimeMillis());
        int row=applyMaterialMapper.receiving(id,date,"arrive",userPhone);
        if(row<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"订单更改失败");
        }
        //组织加库存
        ApplyMaterial applyMaterial=applyMaterialMapper.selectById(id);
        Integer materialId=applyMaterial.getMaterialId();//获取物资id
        Integer number=applyMaterial.getApplyNumber();//申请的数量
        String companyName=applyMaterial.getDepartmentName();//加库存的仓库

        //首先查询该部门下该类物资是否有
        CompanyMaterial companyMaterial=null;
        try {
            companyMaterial=companyMaterialMapper.getByCompanyNameAndMaterialId(companyName,materialId);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(null==companyMaterial){
            companyMaterial.setCompanyName(companyName);
            companyMaterial.setStock(number);
            companyMaterial.setMaterialId(materialId);
            int row2=companyMaterialMapper.addOne(companyMaterial);
            if(row2<1){
                return RespBean.error(RespBeanEnum.UPDATE_ERROR,"订单更改失败");
            }

        }
        //部门下含有该物资，则直接加库存
        int row1=companyMaterialMapper.addStock(companyName,materialId,number);
        if(row1<1){
            return RespBean.error(RespBeanEnum.UPDATE_ERROR,"订单更改失败");
        }
        return RespBean.success();
    }

    @Override
    public RespBean getOrderStatistics(String companyName) {
        return RespBean.success(applyMaterialMapper.getOrderStatistics(companyName));
    }
}
